from data_access.dao import database_dao
from sqlalchemy import create_engine
from service.base_service.logger_service import logger


class DatabaseService:
    def __init__(self):
        self.database_dao = database_dao.DatabaseDAO()

    def get_database_by_database_id(self, dispatch_code, database_id):
        database = self.database_dao.get_database_by_database_id(dispatch_code, database_id)
        return database

    def create_engine(self, database):
        # 构建数据库连接URL
        # 构建数据库连接 URL
        connection_url = f"{database.database_driver}://" \
                         f"{database.database_username}:" \
                         f"{database.database_password}@" \
                         f"{database.database_host}:" \
                         f"{database.database_port}/" \
                         f"{database.database_name}"

        # 输出创建引擎前的信息
        logger.info(f"Creating engine with connection URL: {connection_url}")

        try:
            # 创建数据库引擎
            engine = create_engine(connection_url)
            # 输出创建成功的信息
            logger.info("Engine created successfully.")
            return engine
        except Exception as e:
            # 输出创建失败的错误信息
            logger.error(f"Failed to create engine: {e}")
            raise e
